package com.ruoyi.system.controller.report;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.ReportInfoDto;
import com.ruoyi.system.domain.po.Report;
import com.ruoyi.system.service.report.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评估报告Controller
 *
 * @author Tcode
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/system/report")
public class ReportController extends BaseController
{
    @Autowired
    private IReportService reportService;
    private static final String PATH="url";

    /**
     * 返回所有符合查询条件的评估报告[按照分页展开]
     * 分析下第一个注解的作用
     */
    @PreAuthorize("@ss.hasPermi('system:report:getReportList')")
    @GetMapping("/list")
    public TableDataInfo getReportList(
            @RequestParam(required = false)  String reportName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")  Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize)
    {
        //TODO:基于Mybatis的PageHelper分页插件
        startPage();//这里面包含了对前端分页数据的接收
        List<Report> list = reportService.selectReportList(reportName,startTime,endTime);
        return getDataTable(list);
    }


    /**
     * 根据ID查找单个报告
     */
    @PreAuthorize("@ss.hasPermi('system:report:getReport')")
    @GetMapping(value = "/{id}")
    public AjaxResult getReport(@PathVariable("id") Long id)
    {
        return success(reportService.selectReportById(id));
    }

    /**
     * 删除评估报告{支持批量}
     */
    @PreAuthorize("@ss.hasPermi('system:report:removeReport')")
    @Log(title = "评估报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult removeReport(@PathVariable Long[] ids)
    {
        return toAjax(reportService.deleteReportByIds(ids));
    }


    /**
     * 生成评估报告:按理说应该传入实体的，但为了方便调用就改为参数传递吧
     */
    @PreAuthorize("@ss.hasPermi('system:report:generate')")
    @Log(title = "评估报告", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generate(
            @RequestParam Float evaluationResult,
            @RequestParam String evaluationName,
            @RequestParam String standardName,
            @RequestParam String standardCode,
            @RequestParam Long evaluationResultId)
    {
        // 创建 Report 实体并设置参数
        Report report = new Report();
        report.setEvaluationResult(evaluationResult);
        report.setEvaluationName(evaluationName);
        report.setStandardCode(standardCode);
        report.setEvaluationResultId(evaluationResultId);

        // 获取当前登录用户信息:获取信息异常
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String createUser = authentication.getName(); // 假设用户名是创建者
        report.setReportName("报告_" + standardName);
        report.setReportContentPath(PATH);
        report.setCreateUser(createUser);

        // 调用服务层方法插入报告
        return toAjax(reportService.insertReport(report));
    }

    /**
     * 导出报告为Word文档
     */
    @PreAuthorize("@ss.hasPermi('system:report:exportReportInfoToWord')")
    @GetMapping(value = "/export/{id}")
    public void exportReportInfoToWord(@PathVariable("id") Long id, HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=report.docx");
        try {
            reportService.exportReportToWord(id, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看报告详情
     * */
    @PreAuthorize("@ss.hasPermi('system:report:getReportInfo')")
    @GetMapping(value = "/info/{id}")
    public AjaxResult getReportInfo(@PathVariable("id") Long id)
    {
        ReportInfoDto reportInfo = reportService.selectReportInfoById(id);
        return success(reportInfo);
    }


}
